<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
	session_start();
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
	if($_SESSION["IsAdmin"] == true)
	{
		/*include_once("../lib/ClassSanPham.php");
		include_once("../lib/DataProvider.php");
		$sp = new SanPham;
		$sp->MaSanPham = $_POST["MaSanPham"];
		$sp->TenSanPham = $_POST["TenSanPham"];
		$sp->GiaBan = $_POST["GiaBan"];
		$sp->MaTinhTrangSanPham = 1;
		$sp->SoLuongXem = 0;
		$sp->SoLuongTon = $_POST["SoLuong"];
		$sp->SoLuongBan = 0;
		$sp->MaLoaiSanPham = $_POST["MaLoaiSanPham"];
		$sp->ChatLieu = $_POST["ChatLieu"];
		$sp->Mau = $_POST["Mau"];
		$sp->NgayDang = $_POST["NgayDang"];
		$sp->ChiTiet = $_POST["ChiTiet"];
		$sp->DanhGia = $_POST["DanhGia"];
		$sp->MaNhaSanXuat = 1;
		$sp->MaNguoiDang = $_POST["MaNguoiDang"];;
		$sp->GiaGoc = $_POST["GiaGoc"];
		$sp->MaHienThi = $_POST["MaHienThi"];		
		//Thêm sản phẩm vào cơ sở dữ liệu	
		mkdir("../store/".$sp->MaSanPham, 0777);
		$hinh = "../store/".$sp->MaSanPham."/";
		for($i=1;$i<=$_POST["SoLuongHinh"];$i++)
		{
			$temp = "HinhAnh".$i;
			if(isset($_FILES[$temp]["tmp_name"]))
			{
				move_uploaded_file($_FILES[$temp]["tmp_name"],$hinh.$sp->MaSanPham."_".$i.".jpg");
				echo $hinh.$sp->MaSanPham.'_'.$i.'.jpg';
				echo $temp;
				$temp_hinh = $hinh.$sp->MaSanPham.'_'.$i .'.jpg';
				$sql_img = '
					Insert into hinhanhsanpham(MaSanPham,DuongDan)
					value('.$sp->MaSanPham.',"'.$temp_hinh.'")
				';
				DataProvider::ExecuteQuery($sql_img);
			}
			else
			{
				echo "Lỗi upload hình";
				if ($_FILES[$temp]["error"] > 0)
				  {
				  echo "Error: " . $_FILES[$temp]["error"] . "<br />";
				  }
				else
				  {
					  echo "Upload: " . $_FILES[$temp]["name"] . "<br />";
					  echo "Type: " . $_FILES[$temp]["type"] . "<br />";
					  echo "Size: " . ($_FILES[$temp]["size"] / 1024) . " Kb<br />";
					  echo "Stored in: " . $_FILES[$temp]["tmp_name"];
				  }
			}
		}
		$sql = "
			Update sanpham
			Set
			TenSanPham = '".$sp->TenSanPham."',
			GiaBan = ".$sp->GiaBan.",
			MaTinhTrangSanPham = ".$sp->MaTinhTrangSanPham.",
			SoLuongXem = 0,
			SoLuongTon = ".$sp->SoLuongTon.",
			SoLuongBan = 0,
			MaLoaiSanPham = ".$sp->MaLoaiSanPham.",
			ChatLieu = '".$sp->ChatLieu."',
			ChiTiet = '".$sp->ChiTiet."',
			Mau = '".$sp->Mau."',
			DanhGia = '".$sp->DanhGia."',
			NgayDang = '".$sp->NgayDang."',
			MaNhaSanXuat = 1,
			MaNguoiDang = '".$sp->MaNguoiDang."',
			GiaGoc = ".$sp->GiaGoc.",
			MaHienThi = '".$sp->MaHienThi."'
			where MaSanPham = '".$sp->MaSanPham."'
		";
		DataProvider::ExecuteQuery($sql);
		echo $sql;
		header("Location: QuanLySanPham.php");*/
		
		include_once("lib/ClassSanPham.php");
		include_once("lib/DataProvider.php");
		
		$sp = new SanPham;
		$sp->MaSanPham = $_POST["Id"];
		$sp->TenSanPham = $_POST["TenSanPham"];
		$sp->ThongTin = $_POST["ThongTin"];
		if(isset($_POST["Img"]))
			$sp->HinhSlide = $_POST["Img"];
		else
			$sp->HinhSlide = "";
		
		$root_forder = "../img/";
		//Thêm sản phẩm vào cơ sở dữ liệu	
		//mkdir($root_forder.$sp->MaSanPham, 0777);
		$hinh = $root_forder.$sp->MaSanPham."/";
		if($_POST["UpLaiHinh"] == "true")
		{
			$files = glob($hinh."*.{jpg,gif,png,txt}"); 
			foreach($files as $file) 
				unlink($file); 
			$sql_XoaHinh = '
						Update hinhanh
						set DaXoa = 1
						where MaSanPham = '.$sp->MaSanPham.'
					';
			DataProvider::ExecuteQuery($sql_XoaHinh);
			//move_uploaded_file($_FILES['HinhAnhAvatar']["tmp_name"],$hinh.$sp->MaSanPham.".jpg");
			for($i=1;$i<=$_POST["SoLuongHinh"];$i++)
			{
				$temp = "HinhAnh".$i;
				if(isset($_FILES[$temp]["tmp_name"]))
				{
					move_uploaded_file($_FILES[$temp]["tmp_name"],$hinh.$sp->MaSanPham."_".$i.".jpg");
					echo $hinh.$sp->MaSanPham.'_'.$i.'.jpg';
					echo $temp;
					$temp_hinh = $hinh.$sp->MaSanPham.'_'.$i .'.jpg';
					$sql_img = '
						Insert into hinhanh(MaSanPham,url)
						values('.$sp->MaSanPham.',"'.$temp_hinh.'")
					';
					DataProvider::ExecuteQuery($sql_img);
				}
				else
				{
					echo "Lỗi upload hình";
					if ($_FILES[$temp]["error"] > 0)
					  {
					  echo "Error: " . $_FILES[$temp]["error"] . "<br />";
					  }
					else
					  {
						  echo "Upload: " . $_FILES[$temp]["name"] . "<br />";
						  echo "Type: " . $_FILES[$temp]["type"] . "<br />";
						  echo "Size: " . ($_FILES[$temp]["size"] / 1024) . " Kb<br />";
						  echo "Stored in: " . $_FILES[$temp]["tmp_name"];
					  }
				}
			}
		}
		$sql = "
			Update sanpham
			Set
			TenSanPham = '".$sp->TenSanPham."',
			thongtin = '".$sp->ThongTin."',
			HinhSlide = '".$sp->HinhSlide."'
			where MaSanPham = '".$sp->MaSanPham."'
		";
		DataProvider::ExecuteQuery($sql);
		echo $sql;
		header("Location: QuanLySanPham.php");
	}
?>
</body>
</html>